﻿using System;
using Gj;
using UnityEngine;
using UnityEngine.UI;

public class MiniMapItem : MonoBehaviour
{
    public bool isFixed;
    public bool show;
    public NodeHelper image;
    public RectTransform rect;
    public Element element;

    public void Show()
    {
        show = true;
        gameObject.SetActive(true);
    }

    public void Hide()
    {
        show = false;
        gameObject.SetActive(false);
    }
    public void Init(MiniMap map)
    {
        UpdatePosition(map);
    }

    public void Set(string key)
    {
        image.SetData(key);
    }

    public void Refresh(MiniMap map)
    {
        if (isFixed) return;
        UpdatePosition(map);
    }

    protected virtual void UpdatePosition(MiniMap map)
    {
        var tp = element.GetPosition();
        var p = new Vector2(tp.x, tp.z);
        p.x = p.x * map.ratio;
        p.y = p.y * map.ratio;
        p -= map.offset;
        p.x = Math.Abs(p.x) > map.size.x ? map.size.x * (p.x > 0 ? 1 : -1) : p.x;
        p.y = Math.Abs(p.y) > map.size.y ? map.size.y * (p.y > 0 ? 1 : -1) : p.y;
        rect.anchoredPosition = p;
    }
}